﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api.Ddl;

namespace Me.Prettyprint.Cassandra.Serializers
{
    /// <summary>
    /// BooleanSerializer stores the value is 1 byte: true is 1 and false is 0
    /// </summary>
    public class BooleanSerializer : AbstractSerializer<bool>
    {
        public static BooleanSerializer Instance = new BooleanSerializer();

        public BooleanSerializer()
        {
        }
        public override ComparatorType GetComparatorType()
        {
            return ComparatorType.BOOLEANTYPE;
        }
        override public ComparatorType ComparatorType
        {
            get
            {
                return ComparatorType.BOOLEANTYPE;
            }
        }
        public override byte[] ToBytes(bool obj)
        {
            byte[] bytes = new byte[1];
            bytes[0] = obj ? (byte) 1 : (byte) 0;
            return bytes;
        }
        public override Utils.ByteBuffer ToByteBuffer(bool obj)
        {
            return new ByteBuffer(BitConverter.GetBytes(obj));
        }
        public override bool FromBytes(byte[] bytes)
        {
            if (bytes != null && bytes.Length == 1 && bytes[0] == (byte)1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public override bool FromByteBuffer(ByteBuffer buff)
        {
            return FromBytes(buff.GetBuffer());
        }
    }
}
